
<template>
    <div class="CoronavirusLogin">
        <!-- 登录框 -->
        <div class="login">
            <span class="sysName">新冠肺炎调查管理系统</span>
            <div class="loginName"><el-button type="primary" :plain="city" @click="toCity">市级</el-button> | <el-button
                    type="primary" :plain="region" @click="toRegion">区级</el-button></div>
            <!-- form表单 -->
            <el-form ref="ruleFormRef" :model="loginForm" :rules="rules" label-width="120px"
                class="demo-ruleForm loginForm">
                <el-form-item label="" prop="username">
                    <span class="iconfont icon-user-line"></span>
                    <el-input v-model.number="loginForm.username" type="text" placeholder="请输入用户名" />
                </el-form-item>
                <el-form-item label="" prop="password">
                    <span class="iconfont icon-mima"></span>
                    <el-input v-model="loginForm.password" type="password" placeholder="请输入密码" />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm(ruleFormRef)" :plain="true">登录</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script lang="ts">
import { defineComponent, reactive, ref, toRefs } from 'vue'
import type { FormInstance } from 'element-plus'
import { ElMessage } from 'element-plus'
import axios from '@/util/http'
import { useRouter } from 'vue-router'
import { useStore } from 'vuex'
export default defineComponent({
    name: 'Login',
    setup() {
        const state = reactive({
            city: true,
            region: false,
            // 输入框
            loginForm: reactive({
                username: '',
                password: ''
            }),

        })
        const store = useStore()
        const router = useRouter()

        // 登录区域
        const toCity = () => {
            state.city = false
            state.region = true
            state.loginForm.username = ''
            state.loginForm.password = ''
        }
        const toRegion = () => {
            state.city = true
            state.region = false
            state.loginForm.username = ''
            state.loginForm.password = ''
        }
        const ruleFormRef = ref<FormInstance>()
        // 验证用户名
        const validateUsername = (rule: any, value: any, callback: any) => {
            if (value === '') {
                callback(new Error('请输入用户名！'))
            } else {
                callback()
            }
        }
        // 验证密码
        const validatePassword = (rule: any, value: any, callback: any) => {
            if (value === '') {
                callback(new Error('请输入密码！'))
            } else {
                callback()
            }
        }

        const rules = reactive({
            username: [{ validator: validateUsername, trigger: ['change', 'blur'] }],
            password: [{ validator: validatePassword, trigger: ['change', 'blur'] }],
        })

        // 登录
        const submitForm = (formEl: FormInstance | undefined) => {
            if (!formEl) return
            formEl.validate((valid: any) => {
                if (valid) {
                    if (!state.city) {
                        axios({
                            method: 'post',
                            url: 'city_sys/login',
                            params: {
                                userName: state.loginForm.username,
                                password: state.loginForm.password
                            }
                        })
                            .then(res => {

                                if (res.data.msg == '登录成功') {
                                    localStorage.setItem('token', res.headers.authorization)
                                    localStorage.setItem('userName', res.data.data.citySysUser.userName)
                                    localStorage.setItem('role', '管理员')
                                    localStorage.setItem('headPortrait', res.data.data.citySysUser.headPortrait)
                                    if (!state.city) {
                                        localStorage.setItem('userAuthority', 'city')
                                    }
                                    router.push('/Home')

                                } else {
                                    ElMessage.error('账号或用户名错误！')
                                }
                            })
                    } else {
                        axios({
                            method: 'post',
                            url: 'district_sys/login',
                            params: {
                                userName: state.loginForm.username,
                                password: state.loginForm.password
                            }
                        })
                            .then(res => {
                                if (res.data.msg == '登录成功') {
                                    // token
                                    localStorage.setItem('token', res.headers.authorization)
                                    // 用户数据
                                    localStorage.setItem('userId', res.data.data.districtSysUser.userId)
                                    localStorage.setItem('userName', res.data.data.districtSysUser.userName)
                                    localStorage.setItem('password', res.data.data.districtSysUser.password)
                                    localStorage.setItem('role', res.data.data.districtSysUser.role)
                                    localStorage.setItem('headPortrait', res.data.data.districtSysUser.headPortrait)
                                    localStorage.setItem('area', res.data.data.districtSysUser.area)
                                    // 权限
                                    localStorage.setItem('authority', JSON.stringify(res.data.data.permissionList))

                                    if (state.city) {
                                        localStorage.setItem('userAuthority', 'district')
                                    }
                                    router.push('/Home')

                                } else {
                                    ElMessage.error('账号或用户名错误！')
                                }

                            })
                    }
                } else {
                    console.log('登陆失败!')
                    return false
                }
            })
        }

        return {
            ...toRefs(state),
            toCity,
            toRegion,
            ruleFormRef,
            rules,
            submitForm
        }
    }
})
</script>

<style scoped lang="less">
.CoronavirusLogin {
    width: 100%;
    height: 100%;
    background: #1891ff url('@/assets/wang/images/login.png') no-repeat;
    background-position: right bottom;
    display: flex;
    justify-content: center;
    align-items: center;
}

// 登录框
.login {
    width: 30%;
    height: 520px;
    background: white;
    border-radius: 10px;
    display: flex;
    flex-direction: column;
    justify-content: space-evenly;
    align-items: center;

    // 登录框名称
    .sysName {
        font-size: 25px;
        font-weight: 650;
        color: black;
    }

    // 登录表单
    .el-form {
        height: 320px;
    }

    // 输入框图标
    .el-form-item {
        position: relative;
    }

    // 图标
    .iconfont {
        font-size: 30px;
        z-index: 1;
        position: absolute;
        color: rgb(188, 188, 188);
        top: 27px;
        left: 3px;

    }

    // 输入框
    /deep/ .loginForm .el-form-item__content {
        margin-left: 0px !important;
    }

    .el-input {
        width: 100%;
        height: 45px;
        margin-top: 20px;

    }

    /deep/ .el-input__inner {
        padding-left: 28px;
    }

    .loginName {
        display: flex;
        justify-content: center;
        align-items: center;

        .el-button {
            margin: 0px 10px;
            // background-color: white;
            border: none;
            font-size: 16px;
            // color: black;
        }
    }

    .el-form {

        // 按钮
        .el-button {
            width: 350px;
            height: 45px;
            margin-top: 30px;
        }
    }



}
</style>